/*

     This file is part of glstart.

    glstart is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

    glstart is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>. 
*/

#ifndef TEXTURES_C
#define TEXTURES_C

#include <stdio.h>

#define GL_GLEXT_PROTOTYPES

#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <GL/glcorearb.h>

#include "bmp_reader.h"

unsigned int loadBmpTexture(char* fname) {

  BMPFile* bmp_file = loadBMPFile(fname);
  if(!bmp_file) {
    printf("Error loading bmp file \'%s\'\n", fname);
    return 0;
  };

  unsigned int tex;
  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);

  if(bmp_file->dhdr.bits_per_pixel != 32)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmp_file->dhdr.width,
      bmp_file->dhdr.height, 0, GL_RGB,
      GL_UNSIGNED_BYTE, bmp_file->data);
  else
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bmp_file->dhdr.width,
      bmp_file->dhdr.height, 0, GL_RGBA,
      GL_UNSIGNED_BYTE, bmp_file->data);

  glGenerateMipmap(GL_TEXTURE_2D);
  freeBMPFile(bmp_file);

  return tex;
};

#endif
